package com.elinkway.infinitemovies.http.requesttask;

import android.content.Context;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.CommonBean;
import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.bean.UploadPlayRecordBean;
import com.elinkway.infinitemovies.dao.PlayRecordDao;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.CommonParser;
import com.elinkway.infinitemovies.http.requestInterface.RequestCommonInterface;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: liuxiaojiang(liuxiaojiang@lvideo.com)
 * Date: 2016-03-17
 * Time: 15:46
 * Description: FIXME
 */
public class RequestDoPlayRecordTask extends
        MoviesHttpAsyncTask<CommonBean> {

    private List<PlayRecord> recordList;
    private String type;
    private RequestCommonInterface<CommonBean> mRequestCommonInterface;

    @Override
    public void netNull() {
        super.netNull();
        if("merge".equals(type)){
            ToastUtil.showShortToast(context, context.getResources().getString(R.string.sync_playrecord_fail));
        }
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
        if("merge".equals(type)){
            ToastUtil.showShortToast(context, context.getResources().getString(R.string.sync_playrecord_fail));
        }
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
        if("merge".equals(type)){
            ToastUtil.showShortToast(context, context.getResources().getString(R.string.sync_playrecord_fail));
        }
    }

    public void setRequestListener(RequestCommonInterface<CommonBean> requestCommonInterface) {
        this.mRequestCommonInterface = requestCommonInterface;
    }

    public RequestDoPlayRecordTask(Context context, List<PlayRecord> list, String type) {
        super(context);
        this.recordList = list;
        this.type = type;
    }

    @Override
    public LVideoDataHull<CommonBean> doInBackground() {
        List<UploadPlayRecordBean> uploadList = new ArrayList<UploadPlayRecordBean>();
        if(recordList == null){
            return null;
        }
        UploadPlayRecordBean bean = null;
        for (int i = 0; i < recordList.size(); i++){
            if(recordList.get(i) != null){
                bean = new UploadPlayRecordBean();
                if("merge".equals(type)){
                    bean.setAction("0");
                }else if("delete".equals(type)){
                    bean.setAction("1");
                } else if("add".equals(type)){
                    bean.setAction("0");
                }else{

                }
                bean.setAid(recordList.get(i).getAid());
                bean.setCid("0");
                bean.setVid(recordList.get(i).getVid());
                bean.setPlayTime(recordList.get(i).getSeekHistory() < 1000 ? "0" : recordList.get(i).getSeekHistory() / 1000 + "");
                bean.setUpdateTime(recordList.get(i).getTime() + "");
                bean.setDurationTime("0");
                bean.setSource(recordList.get(i).getSite());
                bean.setProductId(MoviesHttpApi.USER_PRODUCT_ID);
                uploadList.add(bean);
                bean = null;
            }
        }
        LVideoDataHull<CommonBean> ret = MoviesHttpApi.doPlayRecordRequst(
                new CommonParser(), uploadList);
        return ret;
    }

    @Override
    public void onPostExecute(int updateId, CommonBean result) {
        if(result != null){
            if("200".equals(result.getCode())){
                if("merge".equals(type)){
                    ToastUtil.showShortToast(context, context.getResources().getString(R.string.sync_playrecord_success));
                    new RequestUserPlayRecordsTask(context, 0, 200).start();
                }else if("delete".equals(type)){
//                    new PlayRecordDao(context).batchDelete(recordList);
                    if(mRequestCommonInterface != null){
                        mRequestCommonInterface.requestSuccess(updateId, result, MoviesHttpApi.DO_PLAY_RECORD);
                    }
                } else if("add".equals(type)){
                    if(recordList != null && recordList.size() > 0){
                        for (int i = 0; i < recordList.size(); i++){
                            if(recordList.get(i) != null){
                                recordList.get(i).setIsUpload("1");
                            }
                        }
                    }
                    new PlayRecordDao(context).save(recordList);
                }else{

                }
            }
        }else{

        }
    }
}
